package com.baosight.sczx.zm.ca.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.ca.domain.ZMCA4101;


public class ServiceZMCA41 extends ServiceEPBase{
	
	@SuppressWarnings("rawtypes")
	public EiInfo initLoad(EiInfo info)
	{		
		EiInfo outInfo = new EiInfo();
//		info.setMethodParam(MethodParamConstants.sqlName,"ZMCA4101.initQuery");
//		info.setMethodParam(MethodParamConstants.daoEPBaseBean, new ZMCA4101());
//		info.setMethodParam(MethodParamConstants.queryBlock,"initQuery");
//		info.setMethodParam(MethodParamConstants.outDataBlock, "result");
//		outInfo = super.query(info, true);
		
		EiBlock initBlock = new EiBlock("result");
		initBlock.addBlockMeta(new ZMCA4101().eiMetadata);
		Map<String,String> map = new HashMap<String,String>();
		map.clear();
		List initList = dao.query("ZMCA4101.initQuery",map);
		initBlock.addRows(initList);
		outInfo.addBlock(initBlock);
		
		EiBlock userBlock = new EiBlock("userInfo");
		userBlock.addBlockMeta(new ZMCA4101().eiMetadata);
		map.clear();
		map.put("fdUserId", UserSession.getLoginName());
		List resultList = dao.query("ZMCA4101.userQuery",map);
		userBlock.addRows(resultList);
		outInfo.addBlock(userBlock);
		
		EiBlock resgGridBlock = new EiBlock("resgGrid");
		resgGridBlock.addBlockMeta(new ZMCA4101().eiMetadata);
		map.clear();
		map.put("fdUserId", UserSession.getLoginName());
		List resgGridList = dao.query("ZMCA4101.gridQuery",map);
		resgGridBlock.addRows(resgGridList);
		outInfo.addBlock(resgGridBlock);
		
		EiBlock resgGridAddBlock = new EiBlock("resgGridAdd");
		resgGridAddBlock.addBlockMeta(new ZMCA4101().eiMetadata);
		map.clear();
		map.put("fdUserId", UserSession.getLoginName());
		List resgGridAddList = dao.query("ZMCA4101.gridUserQuery",map);
		resgGridAddBlock.addRows(resgGridAddList);
		outInfo.addBlock(resgGridAddBlock); 
		return outInfo ;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo save(EiInfo info)
	{//先行删除，再行添加
		EiBlock blockForDelete = new EiBlock("blockForDelete");
		EiColumn column = new EiColumn("fdUserId");
		blockForDelete.addMeta(column);
		info.addBlock(blockForDelete);
		Map map = new HashMap();
		map.put("fdUserId", UserSession.getLoginName());
		blockForDelete.addRow(map);
		super.update(info,"ZMCA4100.deleteByUserId","blockForDelete");
			
		if(info.getBlock("checkBlockSave")!= null)
		{
			EiBlock block = info.getBlock("checkBlockSave");
			
			for(int i = 0; i < block.getRowCount();i++)
			{
				String primKey = getPrimKey16(); 
				Map row = block.getRow(i);
				row.put("fdInitialdatadetailId", row.get("fdKeyId").toString());
				row.put("fdKeyId", primKey);
				row.put("fdUserId", UserSession.getLoginName().toString());				
				row.put("fdValue", "Y");
			}
			if(block.getRowCount() > 0)
			{
				super.update(info,"ZMCA4100.insert","checkBlockSave");	
			}		
		}
		if(info.getBlock("resgGridAdd")!= null)
		{
			EiBlock block = info.getBlock("resgGridAdd");
			
			for(int i = 0;i < block.getRowCount(); i++)
			{
				String primKey = getPrimKey16(); 
				Map row = block.getRow(i);
				row.put("fdInitialdatadetailId", row.get("fdKeyId").toString());
				row.put("fdKeyId", primKey);
				row.put("fdUserId", UserSession.getLoginName().toString());
				row.put("fdValue", Integer.toString(i));
			}
			if(block.getRowCount() > 0)
			{
				super.update(info,"ZMCA4100.insert","resgGridAdd");
			}
		}
		return info ;
	}
	
	 private String getPrimKey16()
	 {
		 return TablePk.getInstance().getNextValue16();
	 }
}
